<?php session_start();
require_once("connect.php");
$id_hotel=$_SESSION['id_hotel'];
$ID_JENIS_KAMAR=$_SESSION['id_jenis'];
$tgl_in=$_SESSION['cekin'];
$tgl_out=$_SESSION['cekout'];
$qty_room=$_SESSION['qty_room'];
$username=$_SESSION['username'];
$tgl=date('Y-m-d');

$available = array ();
$notAvailable =array ();
//echo"$id_hotel<br>";
//echo"$ID_JENIS_KAMAR<br>";
//echo"$tgl_in<br>";
//echo"$tgl_out<br>";

$tglmerge=$tgl_in.$tgl_out;
//echo"$tglmerge.<br>";
$status='1';
//echo "$status<br>";

$qAvailable=
"select * from kamar
where 
ID_HOTEL='$id_hotel' AND
ID_JENIS_KAMAR='$ID_JENIS_KAMAR'";

$qBooking=
   "select *
	from booking b, KAMAR K
	where
	B.ID_HOTEL='$id_hotel' and
	K.ID_JENIS_KAMAR='$ID_JENIS_KAMAR' AND
	K.ID_HOTEL=B.ID_HOTEL AND
	K.NO_KAMAR=B.NO_KAMAR AND
	B.STATUS =1	";
$qInap=
"	select *
	from inap I, KAMAR K
	where
	K.ID_JENIS_KAMAR='$ID_JENIS_KAMAR' AND
	K.ID_HOTEL=I.ID_HOTEL AND
	K.NO_KAMAR=I.NO_KAMAR AND
	I.ID_HOTEL='$id_hotel' AND
	I.REAL_CHECKOUT IS NULL";
$rs_booking=mysql_query($qBooking);
$rs_inap=mysql_query($qInap);
$rs_available=mysql_query($qAvailable);

	while($dataA=mysql_fetch_array($rs_available))
	{
		if($rs_available)
		{
		$available[] = $dataA['NO_KAMAR'];
		}
	}

	while($data=mysql_fetch_array($rs_booking))
	{
	$RENC_CHECKOUT=$data['RENC_CHECKOUT'];
	$RENC_CHECKIN=$data['RENC_CHECKIN'];
	if($rs_booking)
		{
			if ((( $tgl_in >= $RENC_CHECKIN) &&  ( $tgl_in <= $RENC_CHECKOUT)) 
			|| (( $tgl_out >= $RENC_CHECKIN) &&  ( $tgl_out <= $RENC_CHECKOUT)))
			{
			$status = '0';
			$notAvailable[] = $data['NO_KAMAR'];
			break;
			}
				else
				{
					$jarak = $tgl_in;
					while($jarak == $tgl_out)
					{
					 if (( $jarak >= $RENC_CHECKIN) && ( $jarak <= $RENC_CHECKOUT))
						{
						$status = '0';
						$notAvailable[] = $data['NO_KAMAR'];
						break;
						}
					 $jarak = '$jarak + 1';
					}
				}
			
		}

	}
	
	while($dataI=mysql_fetch_array($rs_inap))
	{
	$CHECK_OUT=$dataI['CHECK_OUT'];
	$CHECK_IN=$dataI['CHECK_IN'];
	
		if($rs_inap)
		{
			if ((( $tgl_in >= $CHECK_IN) &&  ( $tgl_in <= $CHECK_OUT)) 
			|| (( $tgl_out >= $CHECK_IN) &&  ( $tgl_out <= $CHECK_OUT)))
			{
			$status = '0';
			$notAvailable[] = $dataI['NO_KAMAR'];
			break;
			}
				else
				{
					$jarak = $tgl_in;
					while($jarak == $tgl_out)
					{
					 if (( $jarak >= $CHECK_IN) && ( $jarak <= $CHECK_OUT))
						{
						$status = '0';
						$notAvailable[] = $data['NO_KAMAR'];
						break;
						}
					 $jarak = '$jarak + 1';
					}
				}
			
		}

	}

	$i = 0;
	foreach (array_diff($available, $notAvailable) as $key => $value) 
	{ 
		if (!is_null($value)) 
		{ 
		$hasil[$i] = $value;
		$i++; 
		} 
	} 
	// for($i=0;$i<$qty_room;$i++)
	// {
		// echo $hasil[$i];
		
		
	// }
	// die();
	$sql2="insert into transaksi values(NULL,'$username','$tgl')";
	$exe2 = mysql_query($sql2);
	$sql = "select * from transaksi order by no_transaksi desc limit 0,1";	
	$exe = mysql_query($sql);
	$no_trans=mysql_fetch_row($exe);
	for($i=0;$i<$qty_room;$i++)
	{
		echo"";
		$q = "insert into booking values (NULL,'$id_hotel','$hasil[$i]','$no_trans[0]','$tgl_in','$tgl_out',0)";
		$exe=mysql_query($q);
	}

	
?>
